package features.advance.leetcode.linkedlist.easy;

import java.util.*;

/**
 *  168. Excel表列名称
 * 给定一个正整数，返回它在 Excel 表中相对应的列名称。
 *
 * 例如，
 *
 *     1 -> A
 *     2 -> B
 *     3 -> C
 *     ...
 *     26 -> Z
 *     27 -> AA
 *     28 -> AB
 *     ...
 * 示例 1:
 *
 * 输入: 1
 * 输出: "A"
 * 示例 2:
 *
 * 输入: 28
 * 输出: "AB"
 * 示例 3:
 *
 * 输入: 701
 * 输出: "ZY"
 * @author LIN
 * @date 2021-04-23
 */
public class Solution168 {


    public String convertToTitle(int columnNumber) {

        int c = columnNumber;

        Stack<String> s = new Stack();
        while(c > 0){
            s.add((char)((c-1)%26+65)+"");
            c = (c-1) / 26;

        }
        int size = s.size();
        String str = "";
        for (int i1 = 0; i1 < size; i1++) {
            str +=s.pop();
        }
        return  str;
    }

    public static void main(String[] args) {
        int columNumber = 701;
        String s = new Solution168() {
            @Override
            public String convertToTitle(int columnNumber) {
                if(columnNumber==1) return "A";
                List<Character> alphbetList = new ArrayList<>();
                // alphbetList.add('a');
                for(int i=0;i<26;i++){
                    alphbetList.add((char)('A'+i));
                }
                String ret = "";

                while(columnNumber>0){
                    columnNumber--;
                    int res = columnNumber%26;

                    ret = String.valueOf(alphbetList.get(res))+ret;

                    columnNumber/=26;

                }
                return ret;
            }
        }.convertToTitle(columNumber);
        System.out.println(s);
    }
}
